/*$(document).ready(function(){
    function stopScrolling(event) {
        event.preventDefault();
    }
    document.addEventListener('touchmove',stopScrolling,false);
})*/
var path = window.document.location.pathname.substring(0, window.document.location.pathname.substr(1).indexOf('/') + 1);
//禁止滑动
/*
document.body.addEventListener('touchmove', function (event) {
    event.preventDefault();
}, false);
*/
var produvtCode=$("#productCode").text();//商品编号
var storeID=null;
var pandian_ID=null;
//var pandian_person=null;//盘点人
var pdzb_id=null;//产品子表ID
$(function () {
    selectNewPandian();
    selectPdByCode();
    selectStoreIdByCode();
})
function  selectPdByCode() {
    //loadingToast('加载中');
    //alert(produvtCode);
    $.ajax({
        url:path+"/wxpandian/selectPdByCode.do",
        data:{
            product_code:produvtCode,

        },
        dataType:"json",
        type:"post",
        async:false,
        success:function(data){
            //console.log(data);
            $("#pname").text(data.product_name);
            $("#pnum").text(data.product);
            $("#pcolor").text(data.color);
            $("#psize").text(data.size);
            $("#poNum").text(data.shuliang);
            //$("#pyk").text(data.);
            //$('#loadingToast').fadeOut('fast');

        },
        error:function(){
            //console.log("异常")
        }
    })
}

//获取storeID
function  selectStoreIdByCode() {

    $.ajax({
        url:path+"/wxpandian/selectStoreIdByCode.do",
        data:{
            product_code:produvtCode,

        },
       // dataType:"json",
        type:"post",
        success:function(data){
            storeID=data;
            //console.log("storeID"+storeID);
        },
        error:function(data){
            //console.log("获取storeID异常")
        }
    })
}
//获取此次盘点信息selectNewPandian
function  selectNewPandian() {

    $.ajax({
        url:path+"/wxpandian/selectNewPandian.do",
        dataType:"json",
        type:"post",
        success:function(data){
            pandian_ID=data.id;
            var pandianjigou=data.jgid;
        },
        error:function(data){
            dialog('提示','当前尚未开启新的盘点，请在先在电脑开启新盘点');
            window.location.href="weikaiqi.jsp";
        }
    })
}

//新增盘点记录 判断是否为空
function insertPdNewQ() {
    var newShuliang=$("#div").text();
    if (newShuliang==null||""==newShuliang||"null"==newShuliang){
        dialog('提示','当前商品库存未发生改变，是否盘点？',function(){
            insertJLtoPdChildNotUpdate();
        });
    }else {
        insertJLtoPdChild();
    }
}
//若直接点击完成，则原数量盘点
function insertJLtoPdChildNotUpdate() {
    var newShuliang=$("#poNum").text();
    var oldShuliang=$("#poNum").text();
    var yingkui=newShuliang-oldShuliang;
    $.ajax({
        url:path+"/wxpandian/selectRepetition.do",
        data:{
            store_id:storeID,
            pandian_id:pandian_ID,

        },
        dataType:"json",
        type:"post",
        success:function(data){
            var chachongNum=data.resultCount;
            pdzb_id=data.pdzb_id;
            if (chachongNum==0){
                $.ajax({
                    url:path+"/wxpandian/insertJLtoPdChild.do",
                    data:{
                        store_id:storeID,
                        shuliang:newShuliang,
                        pandian_id:pandian_ID,
                        yk:yingkui,

                    },
                    //dataType:"json",
                    type:"post",
                    success:function(){
                        $('#dialog1').remove();
                        toast('成功');
                        setTimeout(function(){
                            window.location.href='wxindex.jsp';
                        },1000);
                    },
                    error:function(){
                        dialog('提示','当前产品缺少参数');
                    }
                })
            }else {
                $('#dialog1').remove();
                newShuliang=$("#div").text();
                if (newShuliang==null||""==newShuliang||"null"==newShuliang){
                    dialog('提示','修改已盘点过的商品，输入数量不能为空')
                }else {
                    dialog('提示','当前商品库存已修改，是否保存？',function(){
                        updatePDZBShuling();
                    });
                }
            }
        },
        error:function(){
            dialog('提示','当前产品缺少参数，请补充后进行盘点操作');
        }
    })

}



function  insertJLtoPdChild() {
    var newShuliang=$("#div").text();
    var oldShuliang=$("#poNum").text();
    var yingkui=newShuliang-oldShuliang;
    $.ajax({
        url:path+"/wxpandian/selectRepetition.do",
        data:{
            store_id:storeID,
            pandian_id:pandian_ID,

        },
        dataType:"json",
        type:"post",
        success:function(data){
            var chachongNum=data.resultCount;
            pdzb_id=data.pdzb_id;
            //console.log("当点重复"+data+"num:"+chachongNum);
            if (chachongNum==0){
                $.ajax({
                    url:path+"/wxpandian/insertJLtoPdChild.do",
                    data:{
                        store_id:storeID,
                        shuliang:newShuliang,
                        //pandianren:pandian_person,
                        pandian_id:pandian_ID,
                        yk:yingkui,

                    },
                    //dataType:"json",
                    type:"post",
                    success:function(){
                        toast('成功');
                        setTimeout(function(){
                            window.location.href='wxindex.jsp';
                        },1000);
                    },
                    error:function(){
                        dialog('提示','当前产品缺少参数');
                    }
                })
            }else {
                //alert("当前已存在盘点已存在，是否覆盖？")
                if (newShuliang==null||""==newShuliang||"null"==newShuliang){
                    dialog('提示','盘点数量不能为空');
                }else {
                    dialog('提示','当前商品库存已修改，是否保存？',function(){
                        updatePDZBShuling();
                    });
                }
            }
        },
        error:function(){
            dialog('提示','当前产品缺少参数，请补充后进行盘点操作');
        }
    })

}
var count=0;
function plusOneCount() {

    var textstr=$("#div").text();
    if (textstr==null||""==count||"null"==count||count==NaN){
        count=0;
    }else {
        count=parseInt(textstr);
    }
    count+=1;
    $("#div").text(count);
}
function mimusOneCount() {

    var textstr=$("#div").text();
    if (textstr==null||""==count||"null"==count||count==NaN||count<=0){
        count=0;
    }else {
        count-=1;
    }
    $("#div").text(count);
}

//修改当前产品数量

function updatePDZBShuling(){
    var newShuliang=$("#div").text();
    var oldShuliang=$("#poNum").text();
    var yingkui=newShuliang-oldShuliang;
    $.ajax({
        url:path+"/wxpandian/updatePDZBShuling.do",
        data:{
            update_shuliang:$("#div").text(),
            yk:yingkui,
            pdzb_id:pdzb_id,

        },
        //dataType:"json",
        type:"post",
        success:function(){
            $('#dialog1').remove();
            toast('成功');
            setTimeout(function(){
                window.location.href='wxindex.jsp';
            },1000);
        },
        error:function(){
            dialog('提示','网络异常，请稍后重试');
        }
    })
}

$(function () {
    var newQ=$("#reqNewQ").val();
    //alert(newQ)
    if (newQ!=null&&""!=newQ&&"null"!=newQ){
        $("#div").text(newQ)
    }
})